﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TestOSMApi.Repositories;

namespace TestOSMApi
{
    public class Program
    {
        private static string format;
        private static string flat;
        private static string flon;
        private static string tlat;
        private static string tlon;

        static void Main(string[] args)
        {
            #region test data working
            format = "geojson";
            flat = "52.215676";
            flon = "5.963946";
            tlat = "52.2573";
            tlon = "6.1799";
            #endregion

            #region test data non-working
            //format = "geojson";
            //flat = "";
            //flon = "";
            //tlat = "";
            //tlon = "";
            #endregion

            RouteResult result = GetRouteOpenStreet(format, flat, flon, tlat, tlon);
        }

        public static RouteResult GetRouteOpenStreet(string format, string flat, string flon, string tlat, string tlon)
        {
            var searchString = string.Format("?format={0}&flat={1}&flon={2}&tlat={3}&tlon={4}", format, flat, flon, tlat, tlon);
            searchString += "&v=motorcar&fast=1&layer=mapnik&instructions=1";

            var sUrl = "http://www.yournavigation.org/api/1.0/gosmore.php" + searchString;

            WebRequest request = HttpWebRequest.Create(sUrl);
            request.Timeout = 10000;
            request.Method = "GET";
            request.ContentType = "application/json";

            Stream dataStream;
            string responseFromServer;
            using (WebResponse response = request.GetResponse())
            {
                dataStream = response.GetResponseStream();
                using (var reader = new StreamReader(dataStream))
                {
                    responseFromServer = reader.ReadToEnd();
                }
            }

            if (!string.IsNullOrEmpty(responseFromServer))
            {
                #region comments
                //var responseObject = JSonUtils.DeserializeObject<RouteResult>(responseFromServer);
                //if (responseObject != null)
                //{
                //    responseObject.success = true;
                //    return responseObject;
                //}
                #endregion

                var responseObject = (RouteResult)JsonConvert.DeserializeObject(responseFromServer, typeof(RouteResult),
                    new JsonSerializerSettings
                    {
                        Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
                        {
                            Debug.WriteLine(args.ErrorContext.Error.Message);
                            args.ErrorContext.Handled = true;
                        }
                    });

                if (responseObject != null)
                {
                    responseObject.Success = true;
                    return responseObject;
                }

                return new RouteResult() { Success = false };
            }

            return new RouteResult() { Success = false };
        }
    }
}
